﻿using Furion.DatabaseAccessor;
using Furion.DataEncryption;
using Furion.DependencyInjection;
using Furion.FriendlyException;
using HOPU.OnlineSchool.Application.Users.Dtos;
using HOPU.OnlineSchool.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HOPU.OnlineSchool.Application.Users.Services
{
    public class UserService:IUserService,ITransient
    {
        private readonly IRepository<AclUser> _repository;

        public UserService(IRepository<AclUser> repository)
        {
            _repository = repository;
        }

        public async Task<LoginInfoOutPut> Login(LoginInfoInput input)
        {
            //获取加密后的字符串
            var password = MD5Encryption.Encrypt(input.PassWord);
            //根据用户名和密码查询
            var user =await _repository.FirstOrDefaultAsync(e => e.Username == input.UserName&&e.Password==password);

            //如果返回为空，则说明用户不存在
            if (user == null)
                throw Oops.Oh("用户名或密码不正确");

            //返回token（临时处理后续优化）
            return new LoginInfoOutPut() { Token = "Admin" };
        }
    }
}
